package com.mchat.communication.server.handler;

import com.mchat.communication.process.frame.FrameProcessorFactory;
import com.mchat.communication.process.frame.FrameType;
import com.mchat.communication.process.message.MessageType;
import com.mchat.communication.process.message.MsgProcessorFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class SocketHandler {
    @Resource
    private FrameProcessorFactory frameProcessorFactory;
    @Resource
    private MsgProcessorFactory msgProcessorFactory;

    public void handle(ChannelHandlerContext ctx, WebSocketFrame frame) {
        FrameType type = FrameType.valueOf(frame.getClass().getSimpleName());
        frameProcessorFactory.getProcessor(type).process(ctx.channel(), frame);
    }

    public void handleLogout(ChannelHandlerContext ctx) {
        msgProcessorFactory.getProcessor(MessageType.LOGOUT).process(ctx.channel(), null);
    }


}